package com.mbc.service.impl;


import com.mbc.dto.DetailsActivityInfoDTO;
import com.mbc.dto.InserTLoveBaseActivityDTO;
import com.mbc.mapper.LoveBaseActivityMapper;
import com.mbc.mapper.SignMapper;
import com.mbc.service.LoveBaseActivityService;
import com.mbc.vo.DetailsActivityInfoVO;
import com.mbc.vo.SimpleActivityVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.UUID;

/**
 * <p>
 * 爱心基地活动表 服务实现类
 * </p>
 *
 * @author suimeng
 * @since 2024-04-19
 */
@Service
public class LoveBaseActivityServiceImpl implements LoveBaseActivityService {

    @Autowired
    private LoveBaseActivityMapper loveBaseActivityMapper;

    @Autowired
    private SignMapper signMapper;

    /**
     * 新增爱心基地活动
     *
     * @param userId                    用户 ID
     * @param inserTLoveBaseActivityDTO inser tlove base 活动 DTO
     * @return boolean
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean insertLoveActivity(Integer userId, InserTLoveBaseActivityDTO inserTLoveBaseActivityDTO) {
        LocalDateTime now = LocalDateTime.now();
        String signId = UUID.randomUUID().toString();
        Integer count = signMapper.insertLoveActivitySign(signId, inserTLoveBaseActivityDTO);
        //获取最大的活动id
        Integer maxActivityId = loveBaseActivityMapper.getByMaxActivityId();
        if (maxActivityId == null) {
            maxActivityId = 100000;
            return loveBaseActivityMapper.insertLoveActivity(maxActivityId, userId, now, signId,
                    inserTLoveBaseActivityDTO) + count > 1;
        }
        maxActivityId = maxActivityId + 1;
        return loveBaseActivityMapper.insertLoveActivity(maxActivityId, userId, now, signId,
                inserTLoveBaseActivityDTO) + count > 1;
    }

    /**
     * 分页查询所有活动
     *
     * @return {@link ArrayList}<{@link SimpleActivityVO}>
     */
    @Override
    public ArrayList<SimpleActivityVO> getBySimpleActivity(Integer page, Integer size) {
        int offest = (page - 1) * size;
        int limit = size;
        ArrayList<SimpleActivityVO> list = loveBaseActivityMapper.getBySimpleActivity(offest, limit);
        return list;
    }

    /**
     * 根据活动id查询爱心基地活动详细信息
     *
     * @param activityId 活动 ID
     * @return {@link DetailsActivityInfoVO}
     */
    @Override
    public DetailsActivityInfoVO getDetailsActivityInfo(Integer activityId) {
        return loveBaseActivityMapper.getDetailsActivityInfo(activityId);
    }

    /**
     * 根据活动id或名称查询爱心基地活动信息
     *
     * @param detailsActivityInfoDTO 详细信息 活动信息 DTO
     * @return {@link ArrayList}<{@link SimpleActivityVO}>
     */
    @Override
    public ArrayList<SimpleActivityVO> getByIdSimpleActivity(DetailsActivityInfoDTO detailsActivityInfoDTO) {
        Integer page = detailsActivityInfoDTO.getPage();
        Integer limit = detailsActivityInfoDTO.getSize();
        Integer offest = (page - 1) * limit;
        return loveBaseActivityMapper.getByIdSimpleActivity(limit,offest,detailsActivityInfoDTO);
    }

    /**
     * 新增爱心活动浏览量
     *
     * @param activityId 活动 ID
     * @return boolean
     */
    @Override
    public boolean updateViewNumber(Integer activityId) {
        return loveBaseActivityMapper.updateViewNumber(activityId) > 0;
    }

    /**
     * 新增爱心活动点赞量
     *
     * @param activityId 活动 ID
     * @return boolean
     */
    @Override
    public boolean updateLikesNumber(Integer activityId) {
        return loveBaseActivityMapper.updateLikesNumber(activityId) > 0;
    }

    /**
     * 更新爱心活动参与人数
     *
     * @param activityId 活动 ID
     * @return boolean
     */
    @Override
    public boolean updateParticipationNumber(Integer activityId) {
        return loveBaseActivityMapper.updateParticipationNumber(activityId) > 0;
    }
}
